package com.xjj.service.impl;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xjj.entity.PageBean;
import com.xjj.entity.Parent;
import com.xjj.entity.Photo;
import com.xjj.mapper.PhotoMapper;
import com.xjj.service.PhotoService;
import com.xjj.util.utils.JwtUtil;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static util.GetCurrentTime.getCurrentTime;

/**
 * @ClassName PhotoServiceImpl
 * @Description TODO
 * @Author 于文龙
 * @Date 2022/10/20 10:38
 * @Version 1.0
 **/
@Service
@Transactional
public class PhotoServiceImpl extends ServiceImpl<PhotoMapper, Photo> implements PhotoService {

    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    private PhotoMapper photoMapper;

    //根据发送者id查询所有相册
    @Override
    public PageBean<Photo> getAllPhotoByMangerId(int id/*班主任id*/, int pageNum, int sizeNum,int parentId) throws JsonProcessingException {
        PageBean<Photo> pageBean= new PageBean<>();
        Page<Photo> p = PageHelper.startPage(pageNum,sizeNum) ;
        List<Photo> data = photoMapper.getAllPhotoByMangerId(id);
            //查询家长是否点赞
        for (Photo datum : data) {
            Integer key = datum.getId();
            Boolean exist = stringRedisTemplate.opsForSet().isMember("photoId"+key, String.valueOf(parentId));
            if(exist){
                datum.setIsLiked(true);
            }else{
                datum.setIsLiked(false);
            }
        }

        pageBean.setData(data);
        pageBean.setCurrPage(pageNum);
        pageBean.setCurrPageSize(data.size());
        pageBean.setTotalNums((int)p.getTotal());
        pageBean.setTotalPages(p.getPages());
        pageBean.setPageSize(sizeNum);
        return pageBean;
    }

    //根据相册id删除相册
    @Override
    public void deletePhoto(Photo photo) {
        photoMapper.deletePhoto(photo.getId());
        photoMapper.deletePhotoPngByPhotoId(photo.getId());
    }

    //发布相册
    @Override
    public void addPhoto(Photo photo) {
        String currentTime = getCurrentTime();
        photo.setTime(currentTime);
        photoMapper.addPhoto(photo);
        List<String> img = photo.getImg();
        for (String s : img) {
            photoMapper.addPhotoPng(photo.getId(),s);
        }
    }

    //模糊查询相册
    @Override
    public PageBean<Photo> getByTimeOrContent(Photo photo, int pageNum, int sizeNum) {
        if(photo.getTime()==null){
            photo.setTime("");
        }else if(photo.getContent()==null){
            photo.setContent("");
        }
        PageBean<Photo> pageBean= new PageBean<>();
        Page<Photo> p = PageHelper.startPage(pageNum,sizeNum) ;
        List<Photo> data = photoMapper.getByTimeOrContent(photo.getManagerId(),photo.getContent());
        pageBean.setData(data);
        pageBean.setCurrPage(pageNum);
        pageBean.setCurrPageSize(data.size());
        pageBean.setTotalNums((int)p.getTotal());
        pageBean.setTotalPages(p.getPages());
        pageBean.setPageSize(sizeNum);
        return pageBean;
    }

    //点赞
    @Override
    public void like(int photoId,int parentId) {
        //判断当前用户是否点赞
        String key = "photoId"+photoId;
        Boolean exist = stringRedisTemplate.opsForSet().isMember(key, String.valueOf(parentId));
        //未点赞
        if(BooleanUtil.isFalse(exist)){
            stringRedisTemplate.opsForSet().add(key,String.valueOf(parentId));
            photoMapper.like(photoId);
        }else{
            //已点赞再次点击则取消点赞
            photoMapper.unlike(photoId);
            stringRedisTemplate.opsForSet().remove(key,String.valueOf(parentId));
        }
    }

    //查看相册详情
    @Override
    public Photo getByPhotoId(int photoId,int parentId) {
        String key = "photoId"+photoId;
        Photo byPhotoId = photoMapper.getByPhotoId(photoId);
        //判断相册是否被当前用户点赞（家长）
        Boolean ismember = stringRedisTemplate.opsForSet().isMember(key, String.valueOf(parentId));
        byPhotoId.setIsLiked(BooleanUtil.isTrue(ismember));
        return byPhotoId;
    }

    //家长查看所有相册（根据班级id找到班主任id通过班主任id查询）
    @Override
    public PageBean<Photo> parentGetAll(int studentId,int pageNum, int sizeNum) {
        PageBean<Photo> pageBean= new PageBean<>();
        Page<Photo> p = PageHelper.startPage(pageNum,sizeNum) ;

        //根据学生id查询班主任id
        int managerId = photoMapper.getManagerId(studentId);
        //根据班主任id查询相册
        List<Photo> data = photoMapper.getAllPhotoByMangerId(managerId);
        pageBean.setData(data);
        pageBean.setCurrPage(pageNum);
        pageBean.setCurrPageSize(data.size());
        pageBean.setTotalNums((int)p.getTotal());
        pageBean.setTotalPages(p.getPages());
        pageBean.setPageSize(sizeNum);
        return pageBean;
    }
}
